﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using Negocio;
namespace Presentacion
{
    public partial class WSequence
    {
        private string sequence;
        private TextBox txSequence;
        private ObservableCollection<Test> testsList;
        public WSequence(string oneSequence, ObservableCollection<Test> oneTestList, TextBox oneTxSequence)
        {
            sequence = "";
            this.InitializeComponent();
            testsList = oneTestList;
            lvwTestForSequence.ItemsSource = testsList;
            txSequence = oneTxSequence;
            txbSequence.Text = "";
            txbSequence.IsEnabled = false;
            if (!oneTxSequence.Text.Equals("n/a"))
            {
                txSequence = oneTxSequence;
                sequence = oneTxSequence.Text;
                txbSequence.Text = sequence;
            }
            // Insert code required on object creation below this point.
        }

        private void btnOne_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "1";
            txbSequence.Text = sequence;
        }

        private void btnTwo_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "2";
            txbSequence.Text = sequence;
        }

        private void btnThree_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "3";
            txbSequence.Text = sequence;
        }

        private void btnFour_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "4";
            txbSequence.Text = sequence;
        }

        private void btnFive_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "5";
            txbSequence.Text = sequence;
        }

        private void btnSix_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "6";
            txbSequence.Text = sequence;
        }

        private void btnSeven_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "7";
            txbSequence.Text = sequence;
        }

        private void btnEight_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "8";
            txbSequence.Text = sequence;
        }

        private void btnNine_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "9";
            txbSequence.Text = sequence;
        }

        private void btnDivide_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "/";
            txbSequence.Text = sequence;
        }

        private void btnMult_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "*";
            txbSequence.Text = sequence;
        }

        private void btnSubstraction_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "-";
            txbSequence.Text = sequence;
        }

        private void btnSum_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "+";
            txbSequence.Text = sequence;
        }

        private void btnParenthesisLeft_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "(";
            txbSequence.Text = sequence;
        }

        private void btnParentesisRight_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + ")";
            txbSequence.Text = sequence;
        }

        private void btnSqrt_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "$";
            txbSequence.Text = sequence;
        }

        private void btnPi_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "?";
            txbSequence.Text = sequence;
        }

        private void btnPow_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "^";
            txbSequence.Text = sequence;
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            Test test=new Test();
            
            if (sequence.Equals(""))
            {
                sequence = "n/a";
                this.Close();
            }
            else
            {
                test.Sequence = sequence;
                if (!Club.getInstance().validateSequence(sequence,testsList))
                {
                    sequence = "";
                    txbSequence.Text = sequence;
                    MessageBox.Show("Error en la fórmula", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    this.Close();
                    txSequence.Text = sequence;
                }
            
            }
            
           // txSequence.Text = sequence;
        }

        private void btnZero_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + "0";
            txbSequence.Text = sequence;
        }

        private void btnComa_Click(object sender, RoutedEventArgs e)
        {
            sequence = sequence + ",";
            txbSequence.Text = sequence;
        }

        private void btnC_Click(object sender, RoutedEventArgs e)
        {
            sequence = "";
            txbSequence.Text = sequence;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            string aux = sequence;
            sequence = "";
            string[] aux2 = aux.Split('[');
            if (aux.Length != 0)
            {
                if (aux[aux.Length - 1].Equals(']'))
                {
                    for (int i = 1; i < aux2.Length - 1; i++)
                    {
                        aux2[i] = "[" + aux2[i];
                    }
                    for (int i = 0; i < aux2.Length - 1; i++)
                    {
                        sequence = sequence + aux2[i];
                    }
                }
                else
                {
                    for (int i = 0; i < aux.Length - 1; i++)
                    {
                        sequence = sequence + aux[i];
                    }
                }
                txbSequence.Text = sequence;
            }
        }

        private void lvwTestForSequence_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Test test = (Test)lvwTestForSequence.SelectedItem;
            sequence = sequence + "[" + test.Name + "]";
            txbSequence.Text = sequence;
        }
    }
}